using System;
using System.Data;
using System.Threading;
using HeadspringExample.Core.Domain.Model;
using HeadspringExample.UI;
using HeadspringExample.UI.Helpers;
using HeadspringExample.UI.Models;
using NUnit.Framework;
using WatiN.Core;

namespace HeadspringExample.UITests
{
    [TestFixture]
    public class BrittleTester : WebTestBase
    {
        [Test]
        public void Should_edit_the_event_name_correctly()
        {
            IDbCommand command = SessionSource.CreateSession().Connection.CreateCommand();
            command.CommandText =
                @"exec sp_executesql N'INSERT INTO [Conference] 
                (Name, SessionCount, AttendeeCount, Id) 
                VALUES (@p0, @p1, @p2, @p3)',
                N'@p0 nvarchar(16),@p1 int,@p2 int,@p3 uniqueidentifier',
                @p0=N'Austin Code Camp',@p1=0,@p2=0,@p3='A307ECC7-FB0E-47DF-B387-9D72009DBC4A'";
            
            command.CommandType = CommandType.Text;
            command.ExecuteNonQuery();
            //needed data is now in application

            Pause();
            Link link = Browser.Link(Find.By("rel", "conferences"));
            link.Click();
            Pause();
            Link link2 = Browser.Link(Find.By("rel", "Edit Austin Code Camp"));
            link2.Click();
            Pause();
            var name = "Name";

            Browser.TextField(Find.ByName(name)).TypeText("Desert Code Camp");
            Pause();
            Browser.Forms[0].Submit();
            Pause();
            Pause();

            Element element = Browser.Element("body", null);
            Assert.AreEqual("conferenceIndex", element.Id);
            Link link3 = Browser.Link(Find.By("rel", "Show Desert Code Camp"));
            Assert.IsNotNull(link3);
        }

        private void Pause()
        {
            Thread.Sleep(TimeSpan.FromSeconds(1));
        }
    }
}